package com.sch.hospital.controller;

import com.sch.hospital.domain.Result;
import com.sch.hospital.utils.AliOSSUtils;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * 文件上传控制器
 */
@Tag(name = "文件相关接口")
@RestController
@CrossOrigin
@RequestMapping("/file")
public class FileController {

    @Autowired
    private AliOSSUtils aliOSSUtils;

    /**
     * 上传图片到阿里云OSS
     * @param file 上传的文件
     * @return 图片访问路径
     */
    @Operation(summary = "上传图片到阿里云OSS")
    @PostMapping("/upload")
    public Result upload(MultipartFile file) {
        try {
            // 调用阿里云OSS工具类进行文件上传
            String url = aliOSSUtils.upload(file);
            // 返回图片访问路径
            return Result.success(url);
        } catch (IOException e) {
            // 上传失败
            return Result.error("文件上传失败: " + e.getMessage());
        }
    }
} 